/*
 * @(#)ElementNode.java   10/01/01
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.mainGUI.TreeTable;

import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.jmodel.specification.Specification;
import fr.ema.arch3d.ref.Fractal;
import fr.ema.arch3d.xoadl.configuration.CompositeComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.ConnectorClassADL;
import fr.ema.arch3d.xoadl.configuration.PrimitiveComponentClassADL;
import fr.ema.arch3d.xoadl.interfaceADL.InterfaceADL;
import fr.ema.arch3d.xoadl.specification.ComponentRoleADL;
import fr.ema.arch3d.xoadl.specification.ConnectionADL;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import javax.swing.tree.DefaultMutableTreeNode;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
class ElementNode extends DefaultMutableTreeNode {

    /** Field description */
    String value;

    /**
     * Constructs...
     *
     * @param component
     */
    public ElementNode(Component component) {
        try {
            if (Fractal.getElementtypeController(component).getElementType().equals("container")) {
                this.userObject = "Assembly";
                this.value      = Fractal.getNameController(component).getFcName();
                ElementNode cmps=new ElementNode("Component Instances", null);
                ElementNode ctrs=new ElementNode("Connector Instances", null);
                this.add(cmps);this.add(ctrs);
                Component[] cs=Fractal.getContentController(component).getFcSubComponents();
                for(int i=0; i<cs.length;i++){
                    if (Fractal.getElementtypeController(cs[i]).getElementType().equals("connector"))
                	ctrs.add(new ElementNode(cs[i]));
                    else if (Fractal.getElementtypeController(cs[i]).getElementType().endsWith("Component")) 
                	cmps.add(new ElementNode(cs[i]));
                }
            } else if (Fractal.getElementtypeController(component).getElementType().equals("connector")) {
                this.userObject = "Connector Instance";
                this.value      = Fractal.getNameController(component).getFcName();
            } else if (Fractal.getElementtypeController(component).getElementType().endsWith("Component")) {
                this.userObject = "Component Instance";
                this.value      = Fractal.getNameController(component).getFcName();
            }
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Constructs ...
     *
     *
     * @param cr
     */
    public ElementNode(ComponentRoleADL cr) {
        InterfaceADL[] itfs    = (InterfaceADL[]) cr.getInterface().toArray();
        ElementNode    itfNode = new ElementNode("Interfaces", null);

        for (int i = 0; i < itfs.length; i++) {
            itfNode.add(new ElementNode(itfs[i]));
        }

        this.add(itfNode);
        this.setUserObject("Component Role");
        this.value = cr.getName();
        this.add(new ElementNode("Behavior", cr.getComponentBehavior()));
    }

    /**
     * Constructs...
     *
     * @param cCmp
     */
    public ElementNode(CompositeComponentClassADL cCmp) {
        this.setUserObject("Component Class");
        this.value = cCmp.getName();

        ElementNode ctNode = new ElementNode("Component Type", cCmp.getComponentType().getName());

        this.add(ctNode);
        this.add(new ElementNode("Implementation configuration",
                                 cCmp.getContent().getName() + cCmp.getContent().getVersion()));
    }

    /**
     * Constructs...
     *
     * @param config
     */
    public ElementNode(Configuration config) {
        this.setUserObject("Configuration");
        this.value = config.getName();

        if (config.getPrimitiveComponentClass().size() != 0) {
            ElementNode cps = new ElementNode("Primitive Component Classes", null);

            this.add(cps);

            for (int i = 0; i < config.getPrimitiveComponentClass().size(); i++) {
                cps.add(new ElementNode((PrimitiveComponentClassADL) config.getPrimitiveComponentClass().get(i)));
            }
        }

        if (config.getCompositeComponentClass().size() != 0) {
            ElementNode ccs = new ElementNode("Composite Component Classes", null);

            this.add(ccs);

            for (int i = 0; i < config.getCompositeComponentClass().size(); i++) {
                ccs.add(new ElementNode((CompositeComponentClassADL) config.getCompositeComponentClass().get(i)));
            }
        }

        if (config.getConnectorClass().size() != 0) {
            ElementNode ctrs = new ElementNode("Connector Classes", null);

            this.add(ctrs);

            for (int i = 0; i < config.getConnectorClass().size(); i++) {
                ctrs.add(new ElementNode((ConnectorClassADL) config.getConnectorClass().get(i)));
            }
        }
    }

    /**
     * Constructs...
     *
     * @param connection
     */
    public ElementNode(ConnectionADL connection) {
        this.userObject = "Connection";
        this.value      = connection.getName();
        this.add(new ElementNode("Required Component Role", connection.getRequiredCmpRoleName()));
        this.add(new ElementNode("Required Component Interface", connection.getRequiredInterfaceName()));
        this.add(new ElementNode("Provided Component Role", connection.getProvidedCmpRoleName()));
        this.add(new ElementNode("Provided Component Interface", connection.getProvidedInterfaceName()));
    }

    /**
     * Constructs...
     *
     * @param ctr
     */
    public ElementNode(ConnectorClassADL ctr) {
        this.setUserObject("Connector Class");
        this.value = ctr.getName();

        ElementNode ctNode = new ElementNode("Connector Type", ctr.getConnectorType().getName());

        this.add(ctNode);
        this.add(new ElementNode("Implementation Class", ctr.getContent()));
    }

    /**
     * Constructs ...
     *
     *
     * @param itf
     */
    public ElementNode(InterfaceADL itf) {
        this.setUserObject("Interface");
        this.value = itf.getName();

        ElementNode signature = new ElementNode("Signature", itf.getSignature());
        ElementNode role;

        if (itf.getRole().equals("required")) {
            role = new ElementNode("Role", "client");
        } else {
            role = new ElementNode("Role", "server");
        }

        this.add(signature);
        this.add(role);
    }

    /**
     * Constructs...
     *
     * @param pCmp
     */
    public ElementNode(PrimitiveComponentClassADL pCmp) {
        this.setUserObject("Component Class");
        this.value = pCmp.getName();

        ElementNode ctNode = new ElementNode("Component Type", pCmp.getComponentType().getName());

        this.add(ctNode);
        this.add(new ElementNode("Implementation Class", pCmp.getContent()));
    }

    /**
     *
     * @param spec get the tree of specification
     */
    public ElementNode(Specification spec) {
        this.setUserObject("Specfication");
        this.value = spec.getName();

        ElementNode crs = new ElementNode("Component Roles", null);

        this.add(crs);

        for (int i = 0; i < spec.getComponentRole().size(); i++) {
            crs.add(new ElementNode((ComponentRoleADL) spec.getComponentRole().get(i)));
        }

        ElementNode connections = new ElementNode("Connections", null);

        for (int i = 0; i < spec.getConnection().size(); i++) {
            crs.add(new ElementNode((ConnectionADL) spec.getConnection().get(i)));
        }
    }

    /**
     * Constructs ...
     *
     *
     * @param compositeNode
     * @param value
     */
    public ElementNode(String compositeNode, String value) {
        this.value = value;
        this.setUserObject(compositeNode);
    }

    /**
     * Method description
     *
     * @return
     */
    public String getValue() {
        return value;
    }
}
